package jp.co.sharp.android.parents.kidsguard.activity.setting.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import jp.co.sharp.android.parents.kidsguard.R;
import jp.co.sharp.android.parents.kidsguard.dto.ContactListDto;
import jp.co.sharp.android.parents.kidsguard.utils.CommonUtils;

public class ContactOptAdapter extends ArrayAdapter<ContactListDto> {

    private int resourceId;

    public ContactOptAdapter(@NonNull Context context, int resource) {
        super(context, resource);
        this.resourceId = resource;
    }

    public ContactOptAdapter(@NonNull Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
        this.resourceId = resource;
    }

    public ContactOptAdapter(@NonNull Context context, int resource, @NonNull ContactListDto[] objects) {
        super(context, resource, objects);
        this.resourceId = resource;
    }

    public ContactOptAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull ContactListDto[] objects) {
        super(context, resource, textViewResourceId, objects);
        this.resourceId = resource;
    }

    public ContactOptAdapter(@NonNull Context context, int resource, @NonNull List<ContactListDto> objects) {
        super(context, resource, objects);
        this.resourceId = resource;
    }

    public ContactOptAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<ContactListDto> objects) {
        super(context, resource, textViewResourceId, objects);
        this.resourceId = resource;
    }

    @Override
    public View getView(int position, View converView, ViewGroup parent) {
        ContactListDto contactListDto = getItem(position);

        View view;
        ViewHolder viewHolder;
        if (null == converView) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.portrait = view.findViewById(R.id.portrait);
            viewHolder.name = view.findViewById(R.id.name);
            viewHolder.phone = view.findViewById(R.id.phone);

            view.setTag(viewHolder);
        } else {
            view = converView;
            viewHolder = (ViewHolder)view.getTag();
        }
        viewHolder.portrait.setImageResource(CommonUtils.chooseImage(Integer.parseInt(contactListDto.getPortrait())));
        viewHolder.name.setText(contactListDto.getName());
        viewHolder.phone.setText(contactListDto.getPhone());
        return view;
    }

    class ViewHolder {
        ImageView portrait;
        TextView name;
        TextView phone;
    }

}
